package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 描述:
 * @date: 2020/8/9 14:22
 * @author: summer
 * version: 1.0
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PatchMapping
    public PageList<Employee> findAll(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.findAll(employeeQuery);
    }

    @DeleteMapping("{id}")
    public AjaxResult delEmployee(@PathVariable("id") Long id){
        try {
            employeeService.delEmployee(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！");
        }
    }

    @PatchMapping("/batchDelEmployee")
    public AjaxResult batchDelEmployee(@RequestBody Long[] id){
        try {
            employeeService.batchDelEmployee(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！");
        }
    }

    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "添加失败");
        }
    }

    @PostMapping
    public AjaxResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败");
        }
    }
}
